package cn.xn.hrms.domain.utils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;

/**
 * StringRedisTemplate帮助类
 *
 * @author yushizhong
 * @date 2023/08/24
 */
@Component("redisUtils")
@Slf4j
public class RedisUtils<T> {

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    private final TimeUnit TIME_UNIT = TimeUnit.SECONDS;

    /**
     * 保存单个对象
     * @param key
     * @param value
     * @param expire
     */
    public void saveObject(String key, T value, long expire) {
        if (Objects.isNull(value)) {
            return;
        }
        try {
            String stringValue = JSON.toJSONString(value);
            stringRedisTemplate.opsForValue().set(key, stringValue, expire, TIME_UNIT);
        } catch (Exception ex) {
            log.error("redis执行set发生异常：", ex.getMessage());
        }
    }

    /**
     * 获取单个对象
     * @param key
     * @param tClass
     * @return
     */
    public T getObject(String key, Class<T> tClass) {
        try {
            String value = stringRedisTemplate.opsForValue().get(key);
            if (StringUtils.isBlank(value)){
                return null;
            }
            return JSON.parseObject(value, tClass);
        } catch (Exception ex) {
            log.error("redis执行get发生异常：", ex.getMessage());
            return null;
        }
    }

    /**
     * 保存list
     * @param key
     * @param value
     * @param expire
     */
    public void saveList(String key, List<T> value, long expire) {
        try{

            if (Objects.isNull(value)) {
                return;
            }
            String stringValue = JSON.toJSONString(value);
            stringRedisTemplate.opsForValue().set(key, stringValue, expire, TIME_UNIT);
        } catch (Exception ex) {
            log.error("redis执行set发生异常：", ex.getMessage());

        }

    }

    /**
     * 获取list
     * @param key
     * @param tClass
     * @return
     */
    public List<T> getList(String key, Class<T> tClass){
        try {
            String value = stringRedisTemplate.opsForValue().get(key);
            if (StringUtils.isEmpty(value)){
                return Collections.emptyList();
            }
            return JSON.parseArray(value, tClass);
        }catch (Exception ex) {
            log.error("redis执行get发生异常：", ex.getMessage());
            return Collections.emptyList();
        }
    }

    /**
     * 保存String
     * @param key
     * @param value
     * @param expire
     */
    public void saveString(String key ,String value, Long expire) {
        try {
            if (Objects.nonNull(expire) && expire.longValue() > 0) {
                stringRedisTemplate.opsForValue().set(key, value, expire, TIME_UNIT);
            }else {
                stringRedisTemplate.opsForValue().set(key,value);
            }

        } catch (Exception ex) {
            log.error("redis执行set发生异常：", ex.getMessage());
        }
    }

    /**
     * 获取String
     * @param key
     * @return
     */
    public String getString(String key){
        try {
            return stringRedisTemplate.opsForValue().get(key);
        } catch (Exception ex) {
            log.error("redis执行get发生异常：", ex.getMessage());
            return null;
        }
    }

    /**
     * 单个对象setIfAbsent
     * @param key
     * @param value
     * @param expire
     * @return
     */
    public boolean setIfAbsent(String key, T value, long expire) {
        try {
            if (Objects.isNull(value)) {
                return false;
            }
            String stringValue = JSON.toJSONString(value);

            return stringRedisTemplate.opsForValue().setIfAbsent(key, stringValue, expire, TIME_UNIT);
        } catch (Exception ex) {
            log.error("redis执行setIfAbsent发生异常：", ex.getMessage());
            return false;
        }
    }

    /**
     * 设置过期时间
     * @param key
     * @param expire
     */
    public void setExpire(String key, long expire){
        try{
            stringRedisTemplate.expire(key, expire, TIME_UNIT);
        } catch (Exception ex) {
            log.error("redis执行setExpire发生异常：", ex.getMessage());

        }
    }

    /**
     * 删除key
     * @param key
     */
    public void deleteKey(String key) {
        try{
            stringRedisTemplate.delete(key);
        } catch (Exception ex) {
            log.error("redis执行delete发生异常：", ex.getMessage());

        }
    }
}

